/*****************************************************************************************************
 *
 * current.cpp
 *
 * Decetects current peak.
 *
 *****************************************************************************************************/
// Include Files
#include "mbed.h"
#include "current.h"
AnalogIn current_sensor_1(p16);
AnalogIn current_sensor_2(p17);
AnalogIn current_sensor_3(p18);
AnalogIn current_sensor_4(p19);
AnalogIn current_sensor_5(p20);
//Global Variables
float last_Current = 0;
float time = 0;
bool check = false;
/*******************************************************************
* Current Monitoring Function
*    This function measures the voltage and caputres the waveform
*    with the maximum to be at 3.3 Volts and having a zero value of
*    1.65 Volts to be able to have both positive and negative part
*    of the sine wave.
********************************************************************/
float current_Monitoring(int circuit_number) {
//(Scale) Need to figure out.
    if (circuit_number ==1)
        return ((current_sensor_1.read()-1.65)/1);
    else if (circuit_number ==2)
        return ((current_sensor_2.read()-1.65)/1);
    else if (circuit_number ==3)
        return ((current_sensor_3.read()-1.65)/1);
    else if (circuit_number ==4)
        return ((current_sensor_4.read()-1.65)/1);
    else if (circuit_number == 5)
        return ((current_sensor_5.read()-1.65)/1);
    else
        return -10000000;
}

/**********************************************************************************************
 * is_Current_Sensor_1_at_Peak
 * Returns a time when the current sensor 1 was at it's peak.
 * Is passed a current  to check if it is the maximum value
 * then stores it as last_Current_1
 *
 *********************************************************************************************/
float is_Current_Sensor_at_Peak(float current,Timer b) {
    if ( current > last_Current) {
        last_Current = current;
        check = true;
    } else if (check == true) {
        if ( current < last_Current) {
            time = b.read_us();
            return last_Current;
        } else {
            check = false;
            return -100000;
        }
    }
}

float current_time_is_(int current_number) {
    return time;

}
void timer_end(Timer e){
    e.stop();
}